package com.tencent.welink.api.cos;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.welink.api.OpenApiTokenChange;
import com.tencent.welink.api.base.WelinkOpenApi;
import com.tencent.welink.api.msg.FileNotify;
import com.tencent.welink.config.BussinessConfig;
import com.tencent.welink.util.CrontabUtil;
import com.tencent.welink.util.HttpPoolUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
 * desc:获取临时上传地址
 * 该接口支持多租户按项目永久存储，最大支持上传单个大小为1G的文件，不支持断点续传。
 * 客户端通过此接口获取临时文件上传的地址，然后对得到的地址发送HTTP PUT请求，
 * 把文件以二进制流的形式附在body上传到该地址。文件在微瓴默认保存时效为1小时，
 * 最大保存90天，超过保存时效后文件将被清理，
 * 请在时效内通过common/fileservice/downloadTemporaryFile接口下载上传的文件。
 * 同一个上传地址，在有效期内，上传多个文件，可用文件是最后一个上传文件。
 * Created by junchihe on 2020/6/4.
 */
public class UploadTemporaryFile extends WelinkOpenApi {
    private static final Logger log = LogManager.getLogger(UploadTemporaryFile.class);
    public UploadTemporaryFile() {
        super("/common/fileservice/uploadTemporaryFile",true);
    }
    @Override
    public void displayResult(String result) {

    }

    public static void main(String[] args) {
        new BussinessConfig();
        CrontabUtil.addTaskInRate(new OpenApiTokenChange(), BussinessConfig.loginDurationMinutes,true);

        //获取临时上传地址
        UploadTemporaryFile uploadTemporaryFile =   new UploadTemporaryFile();
        Map<String,Object> map = uploadTemporaryFile.getExtendsPara();
        log.info("Upload file================**************");
        map.put("file_name","junchihe_test_file");
        String reslut = uploadTemporaryFile.DoHttp();
        //CrontabUtil.shutdown();
       //65fe4f51-b905-440e-8794-3ccdb5bab92c
        JSONObject jsonObject = JSON.parseObject(reslut);
        jsonObject = jsonObject.getJSONObject("data");
        String fileId = jsonObject.getString("file_id");
        String upload_url = jsonObject.getString("upload_url");
        log.info("fileid:{}",fileId);
        log.info("url:{}",upload_url);

        //上传文件
        log.info(HttpPoolUtils.httpPut(upload_url,"D:\\ip.txt"));


        log.info("notify file================**************");
        FileNotify fileNotify = new FileNotify();
        map = fileNotify.getExtendsPara();
        map.put("fileId", fileId);
        map.put("uploadResult","success");
        fileNotify.DoHttp();

        //下载文件
        log.info("Download file================**************");
        DownloadTemporaryFile downloadTemporaryFile =   new DownloadTemporaryFile();
        map = downloadTemporaryFile.getExtendsPara();
        map.put("file_id",fileId);
        reslut = downloadTemporaryFile.DoHttp();
        CrontabUtil.shutdown();

        jsonObject = JSON.parseObject(reslut);
        jsonObject = jsonObject.getJSONObject("data");
        fileId = jsonObject.getString("filename");
        upload_url = jsonObject.getString("downloadUrl");
        log.info("fileid:{}",fileId);
        log.info("url:{}",upload_url);
        log.info(HttpPoolUtils.httpGet(upload_url,null));
    }
}
